//
//  ZYCHistoryVersionViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/26.
//

#import "ZYCHistoryVersionViewController.h"
#import "ZYCHistoryVersionCell.h"
#import "ZYCHistoryVersionDetailViewController.h"
#import "ZYCNoDataView.h"

static NSInteger pageSize = 10;
@interface ZYCHistoryVersionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCHistoryVersionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"历史版本", nil);
    [self.view addSubview:self.tableView];
    [self loadNewData];
}

- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize)
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] getUrlString:Profile_Get_VersionManagement requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = responseObject[@"body"][@"rows"];
                self.dataSource = array;
                if (self.dataSource == 0) {
                    self.tableView.hidden = YES;
                    self.noDataView.hidden = NO;
                }else{
                    self.tableView.hidden = NO;
                    self.noDataView.hidden = YES;
                    [self.tableView reloadData];
                }
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCHistoryVersionCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCHistoryVersionCell class]) forIndexPath:indexPath];
    cell.dict = self.dataSource[indexPath.section];
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 16.0f;
    }
    return 5;;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ZYCHistoryVersionDetailViewController *vc = [ZYCHistoryVersionDetailViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}


- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCHistoryVersionCell class] forCellReuseIdentifier:NSStringFromClass([ZYCHistoryVersionCell class])];
    }
    return _tableView;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSArray array];
    }
    return _dataSource;
}
//- (NSArray *)dataSource {
//    if (!_dataSource) {
//        _dataSource = @[
//                        @{@"title":@"Version V1.0.1",@"content":@"2021-06-06 12:45:00"},
//                        @{@"title":@"Version V1.0.2",@"content":@"2021-06-11 12:45:00"},
//                        ];
//    }
//    return _dataSource;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
